﻿using GameProject.Models;
using GameProject.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace GameProject.Controllers
{
	public class SnakesAndLaddersController : Controller
	{

		private GameInstanceService gameService = new GameInstanceService();

		/// <summary>
		/// Controller function that sends the view for the Snakes and Ladders game to the View. "Authorize" makes it 
		/// available only for those that are logged in.
		/// </summary>
		/// <returns>View</returns>
		[Authorize]
		public ActionResult Index()
		{
			return View();
		}

		/// <summary>
		/// Controller function that sends the view for the Snakes and Ladders game rules to the View.
		/// </summary>
		/// <returns>View</returns>
		public ActionResult SLGameRules()
		{
			return View();
		}

		/// <summary>
		/// Controller function that sends the view for the Snakes and Ladders list of games to the View.
		/// </summary>
		/// <returns>View</returns>
		public ActionResult GameList()
		{
			List<GameInstance> activeGames = gameService.GetWaitingGames().Where(x => x.GameType != null && x.GameType.ID == 1).ToList();
			return View(activeGames);
		}

		/// <summary>
		/// Controller function that sends the view for the Snakes and Ladders create game to the View.
		/// </summary>
		/// <returns>View</returns>
		[Authorize]
		public ActionResult CreateGame()
		{
			return View();
		}

		/// <summary>
		/// POST function to create a game
		/// </summary>
		/// <param name="game">The game being played</param>
		/// <returns>The site with the matching game ID or if it fails, the View for create game</returns>
		[Authorize]
		[HttpPost]
		public ActionResult CreateGame(GameInstance game)
		{
			if (ModelState.IsValid)
			{
				//Gets the game with the ID 1 (Snakes and Ladders)
				GameType gameType = gameService.GetGameTypeById(1);
				game.GameType = gameType;
				gameService.AddGameInstance(game);
				gameService.Save();
				//The player is redirected to the site with the ID of the game 
				return RedirectToAction("Index", new { id = game.ID });
			}
			else
			{
				//If it fails, the player sees the create game view.
				return View();
			}
		}

		/// <summary>
		/// Function to play against the computer
		/// </summary>
		/// <returns>Redi</returns>
		public ActionResult PlayAgainstComputer()
		{
			//Creates a Snakes and Ladders game instance with the current number of players = 1, expected number of players = 2.
			GameInstance game = new GameInstance()
			{
				CurrentNumberOfPlayers = 1,
				ExpectedNumberOfPlayers = 2,
				GameType = gameService.GetGameTypeById(1),
				PlayAgainstComputer = true,
				Status = GameStatus.WAITING_FOR_PLAYERS
			};

			//Adds a piece for the computer player
			game.PieceSnakes.Add(new Piece() { IsComputerPlayer = true });
			//Adds the game and saves to the database
			gameService.AddGameInstance(game);
			gameService.Save();

			//The player is redirected to the site with the ID of the game 
			return RedirectToAction("Index", new { id = game.ID });
		}
	}
}
